<?php
require_once 'models/models_thanhtoan.php';
$tt=new models_thanhtoan();
$mod=$_GET['mod'];

if(isset($mod))
{
	switch ($mod)
	{
		case 'thanhtoan':{
		if(isset($_SESSION['username']))
		{
			if (isset($_SESSION['cart']))
			{
				foreach ($_SESSION['cart'] as $key => $value) {	
			$mh=$tt->getmathang($value['tenmh']);
			$data = $tt->getuser($_SESSION['username']);
			$kh=$tt->getkhachhang($data['TenUser']);	
			if(!$kh){
				$tt->addkhachhang($data['TenUser'],$data['DiaChi'],$data['DienThoai']);
				
			}
			$tt->addHoaDon($kh['MaKH'],date('Y-m-d h:i:s'), $kh['TenKH']);
			$hd=$tt->gethoadon(date('Y-m-d h:i:s'));
			$tt->_addCTHoaHon($mh['MaMH'], $hd['MaHD'], $value['soluong'], $value['gia']);
				}
			require 'views/main/cart/thanhtoan.php';
			break;
		}
		}else 
		{ 
			if (isset($_SESSION['cart']))
			{
				foreach ($_SESSION['cart'] as $key => $value) {	
			$mh=$tt->getmathang($value['tenmh']);
			if($_POST)
			{
				$kh=$tt->getkhachhang($_POST['tenkh']);	
				if(!$kh){
				$tt->addkhachhang($_POST['tenkh'],$_POST['diachi'],$_POST['sdt']);
				
				}
			
				$kh=$tt->getkhachhang($_POST['tenkh']);	
				$tt->addHoaDon($kh['MaKH'],date('Y-m-d h:i:s'), $kh['TenKH']);
				$hd=$tt->gethoadon(date('Y-m-d h:i:s'));
				$tt->_addCTHoaHon($mh['MaMH'], $hd['MaHD'], $value['soluong'], $value['gia']);
				echo '<script> window.location.href="index.php?mod=mua"</script>';
			
			}
				
			
		}
		
		}
		require 'views/main/cart/thanhtoan.php';
			break;
		}
		}
		case 'mua':{
				unset($_SESSION['cart']);
				require_once 'views/main/cart/mua.php';
			break;
		}	
			
	}
}

	

?>
